package com.sttk.secreconeclick.bookkeep.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.BaseAc;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.adapter.AccountBookAdapter;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.databinding.ActivityManageAccountBookBinding;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;
import com.sttk.secreconeclick.bookkeep.utils.DragCallback;

import java.util.ArrayList;
import java.util.List;

public class ManageAccountBookActivity extends BaseAc<ActivityManageAccountBookBinding> implements View.OnClickListener {

    private AccountBookAdapter accountBookAdapter;
    private List<AccountBookBean> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_manage_account_book;
    }

    @Override
    protected void initView() {
        mDataBinding.ivManageAccountBookBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.llAddAccountBook.setOnClickListener(this);

        mDataBinding.rvAccountBookList.setLayoutManager(new LinearLayoutManager(mContext));
        accountBookAdapter = new AccountBookAdapter();
        mDataBinding.rvAccountBookList.setAdapter(accountBookAdapter);

        // 绑定拖动功能
        ItemTouchHelper touchHelper = new ItemTouchHelper(new DragCallback(accountBookAdapter));
        touchHelper.attachToRecyclerView(mDataBinding.rvAccountBookList);

        accountBookAdapter.setListener(new AccountBookAdapter.Ilistener() {
            @Override
            public void clickItem(int pos) {
                SPUtil.putInt(mContext, Content.accountBook, pos);
                accountBookAdapter.setSelPosition(SPUtil.getInt(mContext, Content.accountBook, 0));
                finish();
            }

            @Override
            public void clickEdit(int pos) {
                AddAccountBookActivity.selPosition = pos;
                startActivity(AddAccountBookActivity.class);
            }

            @Override
            public void clickDelete(int pos) {
                if (pos == SPUtil.getInt(mContext, Content.accountBook, 0)) {
                    ToastUtils.showShort(getString(R.string.not_delete_current_account_book));
                    return;
                }
                listShow.remove(pos);
                AppSpUtils.saveAccountBookList(listShow);
                getData();
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivAdd || id == R.id.llAddAccountBook) {
            AddAccountBookActivity.selPosition = -1;
            startActivity(AddAccountBookActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<AccountBookBean> listSp = AppSpUtils.getAccountBookList();
        if (listSp != null && !listSp.isEmpty()) {
            listShow.addAll(listSp);
            accountBookAdapter.setList(listSp);
            accountBookAdapter.setSelPosition(SPUtil.getInt(mContext, Content.accountBook, 0));
            mDataBinding.rvAccountBookList.setVisibility(View.VISIBLE);
            mDataBinding.tvAccountBookNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAccountBookList.setVisibility(View.GONE);
            mDataBinding.tvAccountBookNoData.setVisibility(View.VISIBLE);
        }
    }
}